package com.nuanshui.heatedloan.userinfo.service;

import com.nuanshui.framework.consts.TransactionManagerType;
import com.nuanshui.framework.lang.DateUtil;
import com.nuanshui.framework.lang.SysStringUtils;
import com.nuanshui.framework.res.ResourceFacade;
import com.nuanshui.heatedloan.userinfo.constant.IntegralActionType;
import com.nuanshui.heatedloan.userinfo.constant.LoginAccountType;
import com.nuanshui.heatedloan.userinfo.facade.*;
import com.nuanshui.heatedloan.userinfo.facade.thirdlogin.ThirdUserServiceFactory;
import com.nuanshui.heatedloan.userinfo.model.UserInfo;
import com.nuanshui.heatedloan.userinfo.tool.UserTool;
import com.nuanshui.heatedloan.userinfo.vo.SimplerUserInfoVO;
import com.nuanshui.heatedloan.userinfo.vo.UserIntegralVo;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;

@Service
@Transactional(value=TransactionManagerType.USER_INFO_TRANSACTION_MANAGER)
public class UserLogicServiceImpl  implements UserLogicService {
	@Autowired
	private UserService userService;

	@Autowired
	private UserStateService userStateService;

	@Autowired
	private UserQueryService userQueryService;

	@Autowired
	private UserFansRelationService userFansRelationService;
	
	@Autowired
	private ResourceFacade resourceFacade;

	@Autowired
	private UserTool userTool;
	
	@Autowired
	private  UserIntegralService userIntegralService;

	@Autowired
	@Qualifier("ThirdUserServiceFactory")
	private ThirdUserServiceFactory thirdUserServiceFactory;

	/**
	 * 获取当前登录的用户
	 * @param userId
	 * @param isNowUser
	 * @return
	 */
	@Override
	public UserInfo loadCurrentUserInfo(String userId,boolean isNowUser) {

		UserInfo userInfo = userQueryService.loadUserInfo(userId);
		if(userInfo == null) return  null;
		if(userInfo.getPassword()!=null){
			userInfo.setIsSetLoginPassword(1);
		}else{
			userInfo.setIsSetLoginPassword(0);
		}

		if(userTool.isLoginUser()) {
			String img = userInfo.getSelfhoodImg();
			if(img == null) img = "default";
		    Long l = 	userStateService.isPraisedUser(userId,userId,img);
			if(l == null) l = 0l;
			if(l>0) {
				userInfo.setPraised(1);
			}else {
				userInfo.setPraised(0);
			}
		}else  {
			userInfo.setPraised(0);
		}
		//设置二维码
		String qrCodeKey = userInfo.getQrcode();
		if(   StringUtils.isEmpty(   qrCodeKey ) ){
			// 如果二维码的key为空  则生成二维码
			synchronized (userId) {
				userService.saveUserQrcode(userId);
			}
			qrCodeKey = userService.loadQrcodeByUserId( userId );
		}
	
		userInfo.setQrcode( resourceFacade.resolveUrl (qrCodeKey) );
		userInfo.setIntroImg(resourceFacade.resolveUrl(userInfo.getIntroImg(),false));
		userInfo.setVerifiedInfoImg(resourceFacade.resolveUrl(userInfo.getVerifiedInfoImg()));
		//设置地点
		if(StringUtils.isEmpty(userInfo.getAddress())) {
			if(SysStringUtils.isLogicEmpty(userInfo.getLocation())) {
				userInfo.setAddress("未知地点");
			}else {
				userInfo.setAddress(userInfo.getLocation());
			}
		}
		//设置头像
		userInfo.setAvatarUrl(resourceFacade.resolveUrl(userInfo.getAvatar(),120));
		
		
		setInterestedStatus(userInfo);
		if(isNowUser){
		//设置是否绑定微薄
		userInfo.setIsBindWeibo(thirdUserServiceFactory.GetThirdUserService(LoginAccountType.WEI_BO).loadThirdUser(userId) !=null);
		}
		//设置用户等级
		userInfo.setLevel(userTool.parseUserLevel(userInfo.getScore())+1);
		UserIntegralVo userIntegralVo = new UserIntegralVo();
		userIntegralVo.setScore(userInfo.getScore());
		userTool.parseUserLevel(userIntegralVo);
		userInfo.setLevelName(userIntegralVo.getLevelName());
		if(isNowUser){
		//设置是否签到
		if(userTool.isLoginUser()) {
			Integer time =  userIntegralService.getContinuousTime(IntegralActionType.SIGN,DateUtil.getToday("yyyy-MM-dd"));
			userInfo.setIsSign(time!=null);
			}
		}	
		return userInfo;
	}
	
	/**
	 * 获取一年过户的简单对象
	 * @param userId
	 * @return
	 */
	@Override
	public SimplerUserInfoVO loadSimpleUserInfoVO(String userId,String curUserId) {

		UserInfo userInfo  = userQueryService.loadSimplerUserInfo(userId);
		if(userInfo == null) return null;
		SimplerUserInfoVO simplerUserInfoVO = new SimplerUserInfoVO();
		try {
			BeanUtils.copyProperties(simplerUserInfoVO, userInfo);
			simplerUserInfoVO.setVerified(userInfo.getVerified());
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {		
			e.printStackTrace();
		}
		if(curUserId != null) {		    
			simplerUserInfoVO.setInterested(userFansRelationService.isFans(curUserId,userId));
		}
		simplerUserInfoVO.setAvatarUrl(resourceFacade.resolveUrl(userInfo.getAvatar()));
	    
	    return simplerUserInfoVO;
	}
	
	
	/**
	 * 设置关注状态
	 * @param 
	 * @author guohao
	 * @date 2015年6月15日 下午5:03:13
	 */
	@Override
	@Transactional(value=TransactionManagerType.USER_INFO_TRANSACTION_MANAGER)
	public void setInterestedStatus(UserInfo userInfo) {
		userInfo.setSelf(false);
		userInfo.setInterested(false);
		userInfo.setBeInterested(false);
		if(userTool.isLoginUser())
		{
			String currentUserId = userTool.getCurrentUser().getId();
			if(userInfo.getId().equals(currentUserId)){
				userInfo.setSelf(true);
				userInfo.setInterested(null);
				userInfo.setBeInterested(null);
			}else{
				userInfo.setSelf(false);
				userInfo.setInterested(userFansRelationService.isFans(currentUserId, userInfo.getId()));
				userInfo.setBeInterested(userFansRelationService.isFans(userInfo.getId(), currentUserId));
			}	
		}
	}
	
}
